// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
/* 引入ElementUI */
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
/* 引入Axios */
import axios from 'axios'

Vue.use(ElementUI)

Vue.config.productionTip = false
//将ajax设为全局 
Vue.prototype.axios = axios

//配置axios请求的默认url
axios.defaults.baseURL = "http://localhost:8080/"

// 创建axios对象
let http = axios.create({});

// 二维码生成器
import VueQriously from 'vue-qriously'
Vue.use(VueQriously)

// 配置拦截器
//请求拦截器：在请求发出去之前执行一些特殊的代码，对所有的请求有效
http.interceptors.request.use(
  config => {
    // 统一设置信息：token
    console.log("请求拦截器执行拦截.....");

    let token = window.sessionStorage.getItem("token");

    if(token){
      // 将token放到请求里面
      config.headers.authorization = token;
    }

    // 返回配置对象，发送请求
    return config;
  }
)

// 响应拦截器，如果服务器响应头包含token信息，拦截器会自动存放到本地
http.interceptors.response.use(
  response => {
    console.log("响应拦截器执行....");

    // 获得响应头的token放到本地
    let token = response.headers.authorization;

    if(token){
      window.sessionStorage.setItem("token",token);
    }

    // 放行
    return response;
  }
)

// 将axios设置为Vue的元属性
Vue.prototype.$axios = http

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  components: { App },
  template: '<App/>'
})





//发送短信需要的
import sms from './sms'
Object.defineProperty(Vue.prototype, '$sms', {
  value: sms
})
